package Homework;

import java.util.Iterator;
import java.util.LinkedList;

//测试类   用LinkedList
public class NewsTest2 {
	public static void main(String[] args) {
		// 创建LinkedList集合，NewsTitle对象
		LinkedList<NewsTitle> linkedList = new LinkedList<>();
		// 向LinkedList中添加对象
		linkedList.add(new NewsTitle(1, "两会", "张三"));
		linkedList.add(new NewsTitle(2, "建国71周年", "李四"));
		linkedList.add(new NewsTitle(3, "2020年冬奥会", "王五"));
		linkedList.add(new NewsTitle(4, "新冠肺炎疫情", "赵六"));

		// 输出新闻总数
		System.out.println("新闻总数：" + linkedList.size());
		System.out.println("*******************************************");
		// 逐条打印新闻内容
		// ①用增强型的for输出
		for (NewsTitle i : linkedList) {
			System.out.println(i);
		}
		System.out.println("*******************************************");
		// ②通过下标输出
		for (int i = 0; i < linkedList.size(); i++) {
			System.out.println(linkedList.get(i));
		}
		System.out.println("*******************************************");
		// ③通过Iterator输出
		Iterator<NewsTitle> iterator = linkedList.iterator();
		while (iterator.hasNext()) {
			System.out.println(iterator.next());
		}
		System.out.println("*******************************************");
		//添加新闻头条标题
		linkedList.add(0, new NewsTitle(51, "2020年我国对外贸易总额", "孙七"));
		System.out.println(linkedList);
		System.out.println("*******************************************");
		//获取头天新闻
		NewsTitle first=linkedList.get(0);
		System.out.println("新闻头条："+first);
		System.out.println("*******************************************");
		//获取最末条新闻
		NewsTitle last=linkedList.get(linkedList.size()-1);
		System.out.println("最末条新闻："+last);
		System.out.println("*******************************************");
		//删除新闻
		linkedList.remove(3);
		System.out.println(linkedList);
	}
}
